package com.utils;

import pub.web.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by XDLK on 2016/7/11.
 * <p>
 * Date： 2016/7/11
 */
public class PathUtils {

    public static final String IMG = File.separator + "image";
    public static final String PATH_AVATAR = IMG + File.separator + "avatars"; //用户头像保存路径
    public static final String PATH_USERIMG = IMG + File.separator + "userimgs"; //保存用户个人相片
    public static final String PATH_WORK = IMG + File.separator + "work"; //工作坊目录
    public static final String PATH_WORK_HEAD = PATH_WORK + File.separator + "heads"; //工作坊头像目录

    public static final String PATH_DiSEASE_APPLY = IMG + File.separator + "disease"; //互助资金申请附件目录

    public static final String PATH_NOTICE = IMG + File.separator + "notice"; //宣传公告

    /**
     * 获取当前网站域名
     *
     * @return
     */
    public static String getHostAddr() {
        HttpServletRequest request = ServletUtils.getRequest();
        String appPath = request.getContextPath();
        return request.getScheme() + "://" + request.getServerName() + (request.getServerPort() == 80 ? "" : (":" + request.getServerPort())) + appPath;
    }

    /**
     * 获取网站根目录
     *
     * @return
     */
    public static String getContextPath() {
        return ServletUtils.getRequest().getContextPath();
    }

    public static String getRealPath() {
        return ServletUtils.getRequest().getSession().getServletContext().getRealPath("/");
    }

    /**
     * 获取文件上传绝对路径
     *
     * @param type 文件类型名称
     * @return
     */
    public static String getUploadRealPath(String type) {
        String path = getRealPath() + getUploadUrl(type);
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }

    /**
     * 获取文件上传相对路径
     *
     * @param type
     * @return
     */
    public static String getUploadUrl(String type) {
        return "material/" + type;
    }

    /**
     * 获取上传文件
     *
     * @param url      上传文件名称 （如PathUtils.PATH_。。。）
     * @param filename 原始文件名称（为了获取源文件的后缀名）
     * @param userid   新文件的名称
     * @return
     * @throws IOException
     */
    public static File getUpload(String url, String filename, String userid) throws IOException {
        String path = getRealPath() + "uploads" + url + File.separator;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        path += (format.format(new Date()));
        File pathFile = new File(path);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }

        String fileName = userid + getFileExt(filename);
        path += (File.separator + fileName);
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    /**
     * 根据文件获取 文件上传的目录
     *
     * @param file
     * @return
     */
    public static String getUploadPath(File file) {
        if (file == null) {
            return "";
        }
        String path = file.getAbsolutePath();
        path = path.replace(getRealPath(), "");

        path = getContextPath() + path;

        if (!path.startsWith(File.separator)) {
            path = File.separator + path;
        }

        path = path.replaceAll("\\\\", "/");

        return path;
    }

    public static boolean delFile(File file) {
        if (file != null && file.exists()) {
            return file.delete();
        }
        return true;
    }

    /**
     * 根据文件名，获取文件后缀
     *
     * @param filename
     * @return
     */
    public static String getFileExt(String filename) {
        return filename.substring(filename.lastIndexOf("."), filename.length());
    }

}
